﻿/*
 Author:du
 Time:2017.11.23
*/

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ResLoadPrefabMakerTool
{
    [MenuItem("Assets/[FutureCore]/Maker/生成精灵预设", false, 0)]
    private static void AtlasUIMaker()
    {
        Object[] pathsArr = Selection.GetFiltered(typeof(DefaultAsset), SelectionMode.Assets);
        foreach (Object obj in pathsArr)
        {
            string path = AssetDatabase.GetAssetPath(obj);
            string typeDir = null;
            if (path.Contains("Atlas"))
            {
                typeDir = "Atlas";
            }
            else if (path.Contains("Sprite"))
            {
                typeDir = "Sprite";
            }
            else
            {
                DebugUtil.LogError("精灵路径错误!");
                return;
            }
            DirectoryInfo directoryInfo = new DirectoryInfo(path);

            string dirName = directoryInfo.Name;
            BuildTargetDirPrefab(typeDir, dirName);
            DebugUtil.Log("精灵预设生成完成:" + dirName);
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    [MenuItem("Assets/[FutureCore]/Maker/生成单个精灵预设", false, 0)]
    private static void AtlasUIMakerBySelects()
    {
        Object[] pathsArr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets);

        foreach (var item in pathsArr)
        {
            string path = AssetDatabase.GetAssetPath(item);
            FileInfo file = new FileInfo(path);
            string toTargetPath = path.Replace("ResourcesArt", "Resources");
            toTargetPath = toTargetPath.Replace(".png", ".prefab");
            toTargetPath = toTargetPath.Replace(".jpg", ".prefab");
            string allPath = file.FullName;
            string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
            Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
            GameObject go = new GameObject(sprite.name);
            go.AddComponent<SpriteRenderer>().sprite = sprite;
            string prefabPath = toTargetPath.Substring(toTargetPath.IndexOf("Assets"));
            PrefabUtility.CreatePrefab(prefabPath, go);
            Object.DestroyImmediate(go);
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    private static void BuildTargetDirPrefab(string typeDir, string tarDirName)
    {
        DirectoryInfo rawDir = new DirectoryInfo(Application.dataPath + "/_Game/ResourcesArt/" + typeDir + "/" + tarDirName + "/");
        string toTargetPath = Application.dataPath + "/_Game/Resources/" + typeDir + "/" + tarDirName + "/";

        if (Directory.Exists(toTargetPath))
        {
            Directory.Delete(toTargetPath, true);
        }
        if (!Directory.Exists(toTargetPath))
        {
            Directory.CreateDirectory(toTargetPath);
        }
        List<FileInfo> files = new List<FileInfo>();
        foreach (FileInfo f in rawDir.GetFiles("*.png", SearchOption.AllDirectories))
        {
            files.Add(f);
        }
        foreach (FileInfo f in rawDir.GetFiles("*.jpg", SearchOption.AllDirectories))
        {
            files.Add(f);
        }
        foreach (FileInfo file in files)
        {
            string allPath = file.FullName;
            string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
            Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
            GameObject go = new GameObject(sprite.name);
            go.AddComponent<SpriteRenderer>().sprite = sprite;
            string prefabPath = toTargetPath.Substring(toTargetPath.IndexOf("Assets")) + go.name + ".prefab";
            PrefabUtility.CreatePrefab(prefabPath, go);
            Object.DestroyImmediate(go);
        }
    }
}